﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Reflection;

using SIPO.Web.Support;

namespace SIPO.Web.usercontrols
{
    public partial class searching : System.Web.UI.UserControl
    {

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindOperand();
                BindField();
            }

        }

        #region Properties

        public string ClassName
        {
            get;
            set;
        }

        public string ExcludeField
        {
            set;
            get;
        }

        public string IncludeField
        {
            get;
            set;
        }

        public string AliasField
        {
            get;
            set;
        }

        public string IncludeParameter
        {
            get;
            set;
        }

        #endregion

        private void BindOperand()
        {
            List<string> list = new List<string>();

            if (!string.IsNullOrEmpty(IncludeParameter))
            {
                IncludeParameter = IncludeParameter.Replace(" ", "");
                string[] fields = IncludeParameter.Split(',');

                foreach (string field in fields)
                {
                    list.Add(field);
                }
            }

            list.ForEach(delegate(string field)
            {
                ddlOperand.Items.Add(new ListItem(field, field));
            });

        }

        private void BindField()
        {
            ddlField.Items.Clear();

            if (string.IsNullOrEmpty(ClassName))
            {
                ddlField.Items.Insert(0, new ListItem("-- please define ClassName property --", "-1"));
                return;
            }

            Assembly assembly = Assembly.Load("SIPO.Library.DomainObject");
            Type[] classTypes = assembly.GetTypes();

            foreach (Type cls in classTypes)
            {
                if (cls.IsClass)
                {
                    string fullname = cls.FullName.ToLower();
                    string className = fullname.Substring(fullname.LastIndexOf('.')+1).Replace("+fields", "");

                    if (className.Equals(ClassName.ToLower()) && fullname.Contains("fields")) // Get Field Class from Domain Object
                    {
                        PropertyInfo[] propertyInfos;
                        propertyInfos = cls.GetProperties();

                        Dictionary<string, string> fieldsDicts = new Dictionary<string, string>();

                        foreach (PropertyInfo propertyInfo in propertyInfos)
                        {
                            if (!string.IsNullOrEmpty(ExcludeField))
                            {
                                ExcludeField = ExcludeField.Replace(" ", "");
                                string[] fields = ExcludeField.Split(',');

                                List<string> lowFields = new List<string>();
                                foreach (string field in fields)
                                {
                                    var lowField = field.Replace("_", "").ToLower();
                                    lowFields.Add(lowField);
                                }

                                var lowName = propertyInfo.Name.ToLower();
                                if (!lowFields.Contains(lowName))
                                {
                                    var propValue = propertyInfo.GetValue(cls, null);
                                    var propName = propertyInfo.Name;
                                    fieldsDicts.Add(propName, propValue.ToString());
                                }
                            }
                            else
                            {
                                var propValue = propertyInfo.GetValue(cls, null);
                                var propName = propertyInfo.Name;
                                fieldsDicts.Add(propName, propValue.ToString());
                            }

                        }

                        if (!string.IsNullOrEmpty(IncludeField))
                        {
                            IncludeField = IncludeField.Replace(" ", "");
                            string[] fields = IncludeField.Split(',');

                            foreach (string field in fields)
                            {
                                var key = Helper.String.PropCaseStartLower(field).Replace("_", "");
                                var value = field;
                                fieldsDicts.Add(key, value);
                            }
                        }


                        Dictionary<string, string> aliasDicts = new Dictionary<string, string>();
                        if (!string.IsNullOrEmpty(AliasField))
                        {
                            string[] fields = AliasField.Split(',');
                            foreach (string field in fields)
                            {
                                var _aliasField = field.Substring(field.LastIndexOf('=')+1);
                                var _realField = field.Substring(0, field.LastIndexOf('='));
                                aliasDicts.Add(_realField, _aliasField); // format -> RealFieldName as Key, AliasFieldName as Value
                            }
                        }

                        ddlField.Items.Clear();
                        foreach (KeyValuePair<string, string> kvp in fieldsDicts.OrderBy(x => x.Key))
                        {
                            KeyValuePair<string, string> kvpAlias = aliasDicts.FirstOrDefault(kvps => kvps.Key == kvp.Key);

                            if (!string.IsNullOrEmpty(kvpAlias.Key))
                                ddlField.Items.Add(new ListItem(kvpAlias.Value, kvp.Value));
                            else
                                ddlField.Items.Add(new ListItem(kvp.Key, kvp.Value));
                     
                        }

                        ddlField.Items.Insert(0, new ListItem("-- pilih parameter --", "-1"));

                        break;
                    }


                }
            }
        }

    }

}